package Json2RDF.Surface2RDF.SurfaceWriter;

import Json2RDF.Json2Java.OntologyClasses.CylindricalSurface;
import Json2RDF.Surface2RDF.IndexTable.Face2ResourceIndexTable;
import Json2RDF.Surface2RDF.IndexTable.FaceIndexTable;
import RDFManager.PropertySet;
import RDFManager.RDFHead;
import RDFManager.RDFReader;
import RDFManager.ResourceSet;

import java.util.List;
import java.util.stream.IntStream;

import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;

/**
 * {@code CylindricalSurfaceWriter}类提供将cylindricalSurface写入RDF文件的方法
 */
public class CylindricalSurfaceWriter {

  /**
   * 遍历List中的每个元素，将对应数据属性值写入RDF文件，并该表面写入{@link Face2ResourceIndexTable#faceResourceIdxTbl faceResourceMap}和
   * {@link FaceIndexTable#faceIdxTbl faceMap}
   *
   * @param cylindricalSurfaceList 待写入的cylindricalSurface列表
   */
  public static void writeList(List<CylindricalSurface> cylindricalSurfaceList) {

    IntStream.range(0, cylindricalSurfaceList.size()).forEach(i -> {
      write(cylindricalSurfaceList.get(i), "cylindricalSurface" + (i + 1));
      Face2ResourceIndexTable.faceResourceIdxTbl.computeIfAbsent(cylindricalSurfaceList.get(i), v -> RDFReader.model.getResource(RDFHead.namespace + "cylindricalSurface" + (i + 1)));
      FaceIndexTable.faceIdxTbl.computeIfAbsent(cylindricalSurfaceList.get(i).getId(), v -> cylindricalSurfaceList.get(i));
    });
  }


  private static void write(CylindricalSurface cylindricalSurface, String name) {
    Resource inst = RDFReader.model.createResource(RDFHead.namespace + name);
    RDFReader.model.add(inst, RDF.type, ResourceSet.Face);
    RDFReader.model.add(inst, RDF.type, ResourceSet.cylindricalSurface);
    inst.addLiteral(PropertySet.hasID, cylindricalSurface.getId());
    inst.addLiteral(PropertySet.hasRadius, cylindricalSurface.getRadius());
    inst.addLiteral(PropertySet.hasHeight, cylindricalSurface.getHeight());
    inst.addLiteral(PropertySet.isConvex, cylindricalSurface.isIsConvexity());
    RDFTripleArrayWriter.writeTripleArray(inst, "hasAxisEnd1", cylindricalSurface.getAxisEnd1());
    RDFTripleArrayWriter.writeTripleArray(inst, "hasAxisEnd2", cylindricalSurface.getAxisEnd2());
    RDFTripleArrayWriter.writeTripleArray(inst, "hasAxisVector", cylindricalSurface.getAxisVector());
  }

}
